package com.xhu.controller;

import com.alibaba.fastjson.JSON;
import com.xhu.common.ResponseInfo;
import com.xhu.exceptions.*;
import com.xhu.pojo.Compete;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.util.Map;

import static com.xhu.constant.ResultConstant.FAILED;

/**
 * 全局异常处理类
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionController {

    /**
     * 处理其他异常
     */
    @ExceptionHandler(Exception.class)
    public String otherExceptionHandler(Exception e){
        log.error(e.getMessage(),e);
        return JSON.toJSONString(this.failedResponseInfoFactory("发生未知错误，请联系管理员"));
    }


    /**
     * 对登录时异常进行处理
     */
    @ExceptionHandler(UserLoginException.class)
    public String userLoginExceptionHandler(UserLoginException e){
        log.error(e.getMessage(),e);
        ResponseInfo<Map<String, Object>> responce = this.failedResponseInfoFactory(e);
        //封装成json
        return JSON.toJSONString(responce);
    }


    @ExceptionHandler(SaleplanException.class)
    public String salePlanExceptionHandler(SaleplanException e){
        log.error(e.getMessage(),e);
        ResponseInfo<Map<String, Object>> responce = this.failedResponseInfoFactory(e);
        //封装成json
        return JSON.toJSONString(responce);
    }

    @ExceptionHandler(SalePredictionException.class)
    public String salePredictionExceptionHandler(SalePredictionException e){
        log.error(e.getMessage(),e);
        ResponseInfo<Map<String, Object>> responce = this.failedResponseInfoFactory(e);
        //封装成json
        return JSON.toJSONString(responce);
    }

    @ExceptionHandler(SaleperformException.class)
    public String saleperformExceptionHandler(SaleperformException e){
        log.error(e.getMessage(), e);
        ResponseInfo<Map<String, Object>> responce = this.failedResponseInfoFactory(e);
        //封装成json
        return JSON.toJSONString(responce);
    }

    @ExceptionHandler(SQLException.class)
    public String sqlExceptionHandler(SQLException e){
        log.error(e.getMessage(), e);
        ResponseInfo<Map<String, Object>> responce = this.failedResponseInfoFactory("数据库操作失败，请联系管理员");
        //封装成json
        return JSON.toJSONString(responce);
    }

    /**
     *机会管理服务异常
     */
    @ExceptionHandler(ChanceManageException.class)
    public String chanceManageExceptionHandler(ChanceManageException e){
        log.error(e.getMessage(),e);
        ResponseInfo<Map<String, Object>> responce = this.failedResponseInfoFactory(e);
        //封装成json
        return JSON.toJSONString(responce);
    }

    @ExceptionHandler(CompeteException.class)
    public String competeExceptionHandler(CompeteException e){
        log.error(e.getMessage(),e);
        ResponseInfo<Map<String, Object>> responce = this.failedResponseInfoFactory(e);
        //封装成json
        return JSON.toJSONString(responce);
    }

    /**
     * 提供失败返回对象
     */
    private ResponseInfo<Map<String, Object>> failedResponseInfoFactory(Exception e){
        return new ResponseInfo<>(FAILED,e.getMessage(),null);
    }
    private ResponseInfo<Map<String, Object>> failedResponseInfoFactory(String message){
        return new ResponseInfo<>(FAILED, message,null);
    }

}
